﻿(function () {
    var _inited = false;
    var _handler = "Handlers/ProjectInfoHandler.ashx";

    function _init() {
        _initUI();
        _initRequest(true);
    }

    function _initRequest(flag) {
        //默认隐藏
        $('#btnProjectNew', '#ProjectDialog').hide();
        $('#btnProjectDelete', '#ProjectDialog').hide();
        $('#btnProjectModify', '#ProjectDialog').hide();

        var param = top.IndexScript.getSystemParameter('UserMenuInfo');
        if (param && param != null) {
            for (var i = 0; i < param.length; i++) {
                if (param[i].MenuName == '项目管理') {
                    $('#btnProjectNew', '#ProjectDialog').show();
                    $('#btnProjectDelete', '#ProjectDialog').show();
                    $('#btnProjectModify', '#ProjectDialog').show();
                    break;
                }
            }
        }

        //如果没有默认项目则弹出窗口
        var info = _getServerProjectInfo();
        if (info > 0&&flag) {
            return true;
        }
        /*for (var i = 0; i < info.length; i++) {
        if (info[i].SelectFlag > 0) {
        return true;
        }
        }*/
        top.IndexScript.ProjectDialog.Open(_getProjectInfo(), null);
    }

    function _handlerMsg(msg,flag) {
        switch (msg) {
            case 0:
                if (!flag){
                    alert('操作成功');
                }
                _initRequest(flag);
                break;
            case 100:
                alert('项目存在');
                break;
            case 200:
                alert('盘符存在');
                break;
            default:
                alert('操作失败');
                break;
        }
    }

    function _initUI() {
        $('#btnProjectConfirm', '#ProjectDialog').bind('click', function () {
            var pi = _getSelectProject();
            if (pi != null) {
                //设置为默认项目
                if ($('#cbRememberMe', '#ProjectDialog').attr('checked')) {
                    pi.SelectFlag = 1;
                }
                else {
                    pi.SelectFlag = 0;
                }
                _setData(pi);
                top.IndexScript.ProjectDialog.Close();
                top.location.reload();
            }
            else {
                alert('请选择项目');
            }
        });
        $('#btnProjectNew', '#ProjectDialog').bind('click', function () {
            var cb = function (params) {
                _addData(params);
            };
            top.IndexScript.NewProjectDialog.Open(null, cb);
        });
        $('#btnProjectDelete', '#ProjectDialog').bind('click', function () {
            var pi = _getSelectProject();
            if (pi != null) {
                if (confirm('确定删除该数据吗？')) {
                    _delData(pi);
                }
            }
            else {
                alert('请选择项目');
            }
        });
        $('#btnProjectModify', '#ProjectDialog').bind('click', function () {
            var pi = _getSelectProject();
            if (pi != null) {
                var cb = function (params) {
                    _saveData(params);
                };
                top.IndexScript.NewProjectDialog.Open(pi, cb);
            }
            else {
                alert('请选择项目');
            }
        });
        $('.cbproject', '#tbproject').die().live('click', function () {
            if ($(this).attr('checked')) {
                $('.cbproject', '#tbproject').removeAttr('checked');
                $(this).attr('checked', 'checked');
            }
        });
    }

    function _getSelectProject() {
        var pi = null;
        var cbproject = $('#tbproject', '#ProjectDialog').find('.cbproject');
        if (cbproject && cbproject != null) {
            $(cbproject).each(function () {
                if ($(this).attr('checked')) {
                    var tr = $(this).closest('tr');
                    pi = tr.data();
                    return false;
                }
            });
        }
        return pi;
    }

    function _getServerProjectInfo() {
        var info = null;
        $.ajax({
            type: "get",
            url: _handler + "?action=GetServerProjectInfo" + "&ts=" + new Date().valueOf(),
            dataType: 'json',
            async: false,
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        info = data;
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
        return info;
    }

    function _getProjectInfo() {
        var info = null;
        $.ajax({
            type: "get",
            url: _handler + "?action=GetProjectInfo" + "&ts=" + new Date().valueOf(),
            dataType: 'json',
            async: false,
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        info = data;
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
        return info;
    }

    function _addData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=AddProjectInfo&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data,false);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _delData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=DelProjectInfo&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data, false);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _saveData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=SaveProjectInfo&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data, false);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _setData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=SetProjectInfo&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            async: false,
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data, true);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    this.ProjectInfoMgr = {
        init:
        function () {
            if (!!_inited) return;
            _init();
            _inited = true;
        },
        getProjectInfo: function () {
            return _getProjectInfo();
        },
        getServerProjectInfo: function () {
            return _getServerProjectInfo();
        },
        getSelectProject: function () {
            return _getSelectProject();
        }
    }
})();
$(function () {
    ProjectInfoMgr.init();
});